package com.huike.game.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.base.Game18;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class Game18DescActivity extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ListView lv_game18_left;
    private ListView lv_game18_right;
    private List<Game18> list;
    private ImageView iv_game18_jia;
    private AdapterLeft left;
    private AdapterRight right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game18_desc);
        //        注册Eventbus
        EventBus.getDefault().register(this);
        initView();

        //获取上一个页面传递过来的缓存
        SharedPreferences preferences = getSharedPreferences("game18", MODE_PRIVATE);
        String data = preferences.getString("data", null);
        Log.e("---data",data);

        //获取list 缓存
        SharedPreferences listp = getSharedPreferences("game18List", MODE_PRIVATE);
        String data1 = listp.getString("data", null);

        StringBuffer buffer;
        if(data1 == null){
            buffer  = new StringBuffer();
            buffer.append(data).append("-");
        }else{
            buffer  = new StringBuffer(data1);
            buffer.append(data).append("-");
        }

        listp.edit().putString("data",buffer.toString()).commit();

        SharedPreferences list1 = getSharedPreferences("game18List", MODE_PRIVATE);
        String data2 = list1.getString("data", null);
        Log.e("---list1",data2);

        String[] split = data2.split("-");

        for (int i = 0; i<split.length;i++){
            Game18 json = new Gson().fromJson(split[i], Game18.class);
            list.add(json);

        }

        Collections.reverse(list);




        if(this.list.size() != 0 || this.list != null){
            //        左边渲染
            left = new AdapterLeft();
            lv_game18_left.setAdapter(left);

            right = new AdapterRight();
            lv_game18_right.setAdapter(right);
        }


//        点击添加按钮跳转搜索页面 获取数据 添加缓存
        iv_game18_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Game18 beas) {
        list = new ArrayList<>();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        iv_game18_jia = findViewById(R.id.iv_game18_jia);


        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);


        tv_content_title.setText("查询结果");

        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);

        lv_game18_left = (ListView) findViewById(R.id.lv_game18_left);
        lv_game18_right = (ListView) findViewById(R.id.lv_game18_right);
    }


    class AdapterLeft extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(Game18DescActivity.this, R.layout.game18_left, null);

                holder = new ViewHolder(view);
                view.setTag(holder);

            } else {
                holder = (ViewHolder) view.getTag();
            }

            Game18.DataBean data = list.get(i).getData();

            holder.tv_game18_carNumber.setText(data.getCarNumber());
            holder.tv_game18_points.setText("扣  " + data.getScore() + " 分");
            holder.tv_game18_regNumber.setText("未处理违章  " + data.getViolation() + " 次数");
            holder.tv_game18_finePrice.setText("罚款  " + data.getPrice() + "  元");

            holder.iv_game18_left_jian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if(list.size() == 1){
                        Toast.makeText(Game18DescActivity.this, "不能全部删除哦！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    list.remove(i);
                    left.notifyDataSetChanged();

//                    删除缓存
                    SharedPreferences preferences = getSharedPreferences("game18List", MODE_PRIVATE);
                    String preferencesString = preferences.getString("data", null);

                    String[] split = preferencesString.split("-");
                    ArrayList<String> strings = new ArrayList<>();
                    for(int j = 0; j <split.length;j++){
                        strings.add(split[i]);
                    }

                    Collections.reverse(strings);
                    strings.remove(i);

                    StringBuffer buffer = new StringBuffer();
                    for(int p =0;p <strings.size(); p++){
                        buffer.append(strings.get(p)).append("-");
                    }

                    preferences.edit().putString("data",buffer.toString()).commit();


                }
            });


            return view;
        }


    }

    class AdapterRight extends BaseAdapter {

        @Override
        public int getCount() {
            return list.get(0).getData().getDesc().size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(0).getData().getDesc().get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(Game18DescActivity.this, R.layout.game18_right, null);
                 holder = new ViewHolder(view);
                    view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }


            Game18.DataBean.DescBean bean = list.get(0).getData().getDesc().get(i);

            SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            String date = format.format(new Date(Long.parseLong(bean.getTime())));

            holder.tv_game18_time.setText(date);
            if(bean.getState() == 0){
                holder.tv_game18_state.setText("未处理");
            }else{
                holder.tv_game18_state.setText("已处理");
            }
            holder.tv_game18_way.setText(bean.getWay());
            holder.tv_game18_wayDesc.setText(bean.getViolationDesc());
            holder.tv_game18_descPoints.setText("扣分： "+bean.getScore() +"  分");
            holder.tv_game18_descFine.setText("罚款： "+bean.getPrice()+"  元");


//            跳转建通抓拍详细页面
            holder.ll_game18_toImageDesc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    startActivity(new Intent(Game18DescActivity.this,Game18DescImageActivity.class));
                }
            });

            return view;
        }

    }


    class ViewHolder {
        public View rootView;
        public TextView tv_game18_carNumber;
        public TextView tv_game18_regNumber;
        public TextView tv_game18_points;
        public TextView tv_game18_finePrice;
        public ImageView iv_game18_left_jian;

        public TextView tv_game18_time;
        public TextView tv_game18_state;
        public TextView tv_game18_way;
        public TextView tv_game18_wayDesc;
        public TextView tv_game18_descPoints;
        public TextView tv_game18_descFine;
        public LinearLayout ll_game18_toImageDesc;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_game18_carNumber = (TextView) rootView.findViewById(R.id.tv_game18_carNumber);
            this.tv_game18_regNumber = (TextView) rootView.findViewById(R.id.tv_game18_regNumber);
            this.tv_game18_points = (TextView) rootView.findViewById(R.id.tv_game18_points);
            this.tv_game18_finePrice = (TextView) rootView.findViewById(R.id.tv_game18_finePrice);
            this.iv_game18_left_jian =  rootView.findViewById(R.id.iv_game18_left_jian);

            this.tv_game18_time = (TextView) rootView.findViewById(R.id.tv_game18_time);
            this.tv_game18_state = (TextView) rootView.findViewById(R.id.tv_game18_state);
            this.tv_game18_way = (TextView) rootView.findViewById(R.id.tv_game18_way);
            this.tv_game18_wayDesc = (TextView) rootView.findViewById(R.id.tv_game18_wayDesc);
            this.tv_game18_descPoints = (TextView) rootView.findViewById(R.id.tv_game18_descPoints);
            this.tv_game18_descFine = (TextView) rootView.findViewById(R.id.tv_game18_descFine);
            this.ll_game18_toImageDesc = (LinearLayout) rootView.findViewById(R.id.ll_game18_toImageDesc);
        }

    }

}
